package ShuZu;

public class XiShuShuZu {

    public static void main(String[] args) {
        int [][] a=new int[11][11];
        a [1][2]=1;
        a [2][3]=2;
        System.out.println("输出原始的数组：");
        for (int[] ints : a) {
            for (int anInt : ints) {
                System.out.print(anInt+"\t");
            }
            System.out.println();
        }
        //转换为稀疏数组
        //1.确定有效值个数
        //2.创建一个稀疏数组
        //3.遍历二维数组，将特殊值存放在稀疏数组中
        int sum=0;//1.确定有效值个数
        for (int i = 0; i <11 ; i++) {
            for (int j = 0; j <11 ; j++) {
                if(a[i][j]!=0){
                    sum++;
                }
            }
        }
        System.out.println("有效值的个数为："+sum);

        int[][] b=new int[sum+1][3]; //2.创建一个稀疏数组
        b[0][0]=11;
        b[0][1]=11;
        b[0][2]=sum;

        int count=0; //3.遍历二维数组，将特殊值存放在稀疏数组中
        for (int i = 0; i <a.length ; i++) {
            for (int j = 0; j <a[i].length ; j++) {
                if(a[i][j]!=0){
                    count++;
                    b[count][0]=i;
                    b[count][1]=j;
                    b[count][2] = a[i][j];
                }
            }
        }
        System.out.println("稀疏数组为：");

        for (int[] ints : b) {
            for (int anInt : ints) {
                System.out.print(anInt+"\t");
            }
            System.out.println();
        }

        //还原稀疏数组
//        1.读取稀疏数组
        int[][]c= new int[b[0][0]][b[0][1]];
//        2.还原值
        for (int i = 1; i <b.length ; i++) {
            c[b[i][1]][b[i][2]]=b[i][2];
        }
        System.out.println("输出还原的数组：");
        for (int[] ints : a) {
            for (int anInt : ints) {
                System.out.print(anInt+"\t");
            }
            System.out.println();
        }
    }
}
